package band.wukong.practice.java.core.topic.concurrency.atomic;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author wukong(wukonggg@139.com)
 */
public class AtomicReferenceSample {

    public static AtomicReference<Immortal> atomicUserRef = new AtomicReference<>();


    public static void main(String[] args) {

        Immortal immortal = new Immortal("sanzang", 18);
        atomicUserRef.set(immortal);
        System.out.println(atomicUserRef.get());

        Immortal immortal2 = new Immortal("wukong", 25000);
        System.out.println(atomicUserRef.compareAndSet(immortal2, immortal2));
        System.out.println(atomicUserRef.get());

        System.out.println(atomicUserRef.compareAndSet(immortal, immortal2));
        System.out.println(atomicUserRef.get());

    }


    static class Immortal {
        private String name;
        private int age;

        public Immortal(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Immortal{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
